<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<#include "../../base/_meta.html"> 
<@head>
<script type="text/javascript">
	var eee;
	var dialog = frameElement.dialog;
	$(function() {		
		$.metadata.setType("attr", "validate");
        var v = $("form").validate({
            debug: true
        });
        $("#navtab").ligerTab();
        var items=[{ text: '<font color="red">增加或编辑字段信息后，请【保存到库】</font>'},// 红色字体
        		   {text: '增加',click:addRow, icon: 'add' },
                   { text: '删除', click:delRow, icon: 'delete' },
        		   { line: true },
        		   { text: '保存到库', click:doApply,icon: 'save' }];
    	 
        window['g'] = $("#maingrid").ligerGrid({
            columns: [
            { display: '字段名', name: 'fieldid', align: 'left', width: 80, editor: { type: 'text' }},
            { display: '显示名', name: 'fieldname',align: 'left',  width: 80, editor: { type: 'text' } },
            { display: '类型', name: 'fieldtype', width: 60,
            	editor: { type: 'select', data: fieldtypeData, valueField: 'fieldtype'}, 
            	render: function (item){                    
                    return item.fieldtype;
                }
            }, 
            { display: '长度', name: 'fieldlength', type: 'int', width: 40,editor: { type: 'int'} }, 
            { display: '默认值', name: 'defaultvalue', width: 50,editor: { type: 'int'} }, 
            { display: '必须', name: 'ismust', width: 40,
                editor: { type: 'select', data: ismustData, valueField: 'ismust' },
                render: function (item){
                    if (parseInt(item.ismust) == 1) return '是';
                    return '否';
                }
			}, 
            { display: '主键', name: 'iskey', width: 40, editor: { type: 'select', data: iskeyData, valueField: 'iskey' },
                render: function (item){
                    if (parseInt(item.iskey) == 1) return '是';
                    return '否';
                } },
            { display: '备注', name: 'remark',minWidth: 60, width: 112,editor: { type: 'text' }  }
            ],  pageSize:20,
            url: '/ddesign/field/pagenate', 
            enabledEdit: true,
            width: '510',height:'398px',
            toolbar: { items: items}

        	});
        $("form").ligerForm();
	});
	// 增加行
	function addRow(){
		var manager = $("#maingrid").ligerGetGridManager();
        manager.addRow({
        	fieldid : "new field",
        	fieldname: "新字段",
        	fieldtype: 'varchar',
        	fieldlength:0,
        	defaultvalue :0 ,
        	ismust : 0,
        	iskey: 0,
        	remark : "空"
        });
	}
	// 删除行
	function delRow(){
		var sRow = g.getSelectedRows();
		if(sRow.length == 0){
			$.ligerDialog.warn('请至少选择一行进行删除。');
		}else{
			var keys="";
			for(var i=0;i<sRow.length;i++){
				keys += sRow[i].projectcode+",";
			}		
			keys =keys.substring(0,keys.length-1);// remove last ',' 
			$.ligerDialog.confirm('您确定要删除选择的【'+sRow.length+'】条数据吗？', function (yes) { 
				if(yes){
					$.ajax({
						url:"/ddesign/filed/delete?keys="+keys,
						success:function(msg){
				        	if(msg >= 1){
				        		var manager = $("#maingrid").ligerGetGridManager();
								manager.deleteSelectedRow();
				        	}else{
				        		$.ligerDialog.error('删除数据失败。') 
				        	}
				    	}
					});
				}
			});		
		}
	}
	// 保存更新
	function doUpdate(){
		var manager = $("#maingrid").ligerGetGridManager();
        var dataEdit = manager.getUpdated();
        $.ajax({
			url:"/ddesign/field/saveUpdate",
			datatype:"json",
			async:false,
			contentType: 'application/json',
			data:"dataEdit=" + JSON.stringify(dataEdit),
			success:function(msg){
	        	if(msg != "1"){
	        		$.ligerDialog.error(msg); 
	        	}
	    	}
		});	
    }
   	// 保存添加
    function doAdd(){
    	var manager = $("#maingrid").ligerGetGridManager();
    	var dataAdd = manager.getAdded();
		if(dataAdd.length > 0){// 有增加数据
        	$.ajax({
    			url:"/ddesign/field/saveAdd",
    			datatype:"json",
    			async:false,
    			contentType: 'application/json',
    			data:"dataAdd=" + JSON.stringify(dataAdd),
    			success:function(msg){
    	        	if(msg != "1"){
    	        		$.ligerDialog.error(msg); 
    	        	}
    	    	}
    		});	
        }
    }

	function doApply(){
		doUpdate();
		doAdd();
	}
	// Save
	function doSave(){
		var form = liger.get('form_edit');
        if (!form.valid()) {
       		return;
        }
		$.ajax({
			url:"/ddesign/table/save",
			data:$("#form_edit").serialize(),
			success:function(msg){
	        	if(msg == "1"){
	        		parent.window.relaodTree();// Rfresh grid of users
	        		dialog.close();	
	        	}else{
	        		$.ligerDialog.error(msg); 
	        	}
	    	}
		});	
	}
</script>
<style type="text/css">
 body{ font-size:12px;}
 .l-table-edit {}
 .l-table-edit-td{ padding:4px;}
 .l-button-submit,.l-button-test{width:60px; float:center}
 .l-verify-tip{ left:230px; top:120px;}
</style>
</@head>
<body style="overflow-x: hidden; padding: 2px;">
 <form id="form_edit" class="liger-form" data-validate="{}"> 
	<input type="hidden" name="screen" id="screen" value="${(screen)!}"/>
	<input type="hidden" name="projectid" id="projectid" value="${(r.projectcode)!}"/>
	<input type="hidden" name="diagramid" id="diagramid" value="${(r.diagramid)!}"/>
	<input type="hidden" name="tableid" id="tableid" value="${(r.tableid)!}"/><!-- 添加时为空，点击保存后赋值 -->
	
	<div id="navtab" style="width: 500px; height:440px;heioverflow:hidden;">
		<div tabid="base" title="基本信息" lselected="true"  style="width: 500px; height:400px;" >
			<table cellpadding="0" cellspacing="0" class="l-table-edit" width="100%" >
				<tr>
					<td align="right" class="l-table-edit-td" >项目名称:</td>
					<td align="left" class="l-table-edit-td" style="width: 160px">
						${(r.projectname)!}
					</td>
					<td align="left"></td>
					<td align="left"></td>
					
				</tr>
				<tr>
					<td align="right" class="l-table-edit-td" >图表名称:</td>
					<td align="left" class="l-table-edit-td" style="width: 160px">
						${(r.diagramname)!}
					</td>
					<td align="left"></td>
					<td align="left"></td>
				</tr>
				<tr>
					<td align="right" class="l-table-edit-td" >物理表名:</td>
					<td align="left" class="l-table-edit-td" style="width: 160px">
					<input
						name="tablepname" type="text" id="tablepname" ltype="text"
						validate="{required:true,minlength:3,maxlength:100}" value="${(tbl.tablepname)!}"/>
					<#if screen == "E">
					<input
						name="tableid" type="hidden" id="diagramid" value="${(tbl.tableid)!}"/>
					</#if>
					</td>
					<td align="left"></td>
					<td align="left"></td>
				</tr>
				<tr>
					<td align="right" class="l-table-edit-td" >逻辑表名:</td>
					<td align="left" class="l-table-edit-td" style="width: 160px">
					<input
						name="tablelname" type="text" id="tablelname" ltype="text"
						validate="{required:true,minlength:3,maxlength:100}" value="${(tbl.tablelname)!}"/>
					</td>
					<td align="left"></td>
					<td align="left"></td>
				</tr>
				
				<tr>
		           <td align="right" class="l-table-edit-td">备注描述:</td>
		           <td align="left" class="l-table-edit-td" colspan="2" > 
		           <textarea name="remark" id="remark" cols="40" rows="15" 
		           			class="l-textarea"  style="width:400px">${(tbl.remark)!}</textarea>
		           </td> <td align="left"></td>
		       </tr>
				<tr>
				     <td align="center" colspan="4" width="500">
					<input type="button" value="保存" class="l-button l-button-test" onclick="doSave();"/>
				   </td>
		      </tr>
			</table>
		</div>
		<div  title="字段信息" style="width: 545px; height:400px;">
			<div id="maingrid" style="margin:0; padding:0"></div>
		</div>
		<div title="SQL语句">
		
		</div>
		<div title="关系表">
		
		</div>
</div>
	
</form>
<script type="text/javascript">
// if has radio,select,checkbox,put here for init
</script>
</body>
</html>